package com.wetao.flexiblerichtextview.web;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.tencent.sonic.sdk.SonicDiffDataCallback;

import org.json.JSONObject;

public class SonicJavaScriptInterface_x5 {

    private final SonicSessionClientImpl_x5 sessionClient;

    private ObserverJSAction observerJSAction;

    private final Intent intent;

    public static final String PARAM_CLICK_TIME = "clickTime";

    public static final String PARAM_LOAD_URL_TIME = "loadUrlTime";

    public SonicJavaScriptInterface_x5(SonicSessionClientImpl_x5 sessionClient, Intent intent) {
        this.sessionClient = sessionClient;
        this.intent = intent;
    }


    @JavascriptInterface
    public void reLoginDialog(){
        if(observerJSAction != null) {
            observerJSAction.reLoginDialog();
        }
    }

    @JavascriptInterface
    public void jsCallAndroidTest1(String msg){
        Log.e("X5", "jsCallAndroidTest1 -- " + msg);
    }
    @JavascriptInterface
    public void jsCallAndroidTest2(Object msg){
        Log.e("X5", "jsCallAndroidTest2 -- " + msg.toString());
    }





    /*
     * * From RFC 4627, "All Unicode characters may be placed within the quotation marks except
     * for the characters that must be escaped: quotation mark,
     * reverse solidus, and the control characters (U+0000 through U+001F)."
     */
    private static String toJsString(String value) {
        if (value == null) {
            return "null";
        }
        StringBuilder out = new StringBuilder(1024);
        for (int i = 0, length = value.length(); i < length; i++) {
            char c = value.charAt(i);


            switch (c) {
                case '"':
                case '\\':
                case '/':
                    out.append('\\').append(c);
                    break;

                case '\t':
                    out.append("\\t");
                    break;

                case '\b':
                    out.append("\\b");
                    break;

                case '\n':
                    out.append("\\n");
                    break;

                case '\r':
                    out.append("\\r");
                    break;

                case '\f':
                    out.append("\\f");
                    break;

                default:
                    if (c <= 0x1F) {
                        out.append(String.format("\\u%04x", (int) c));
                    } else {
                        out.append(c);
                    }
                    break;
            }

        }
        return out.toString();
    }


    public void setObserverJSAction(ObserverJSAction observerJSAction) {
        this.observerJSAction = observerJSAction;
    }

    public interface ObserverJSAction{
        //重新登录
        void reLoginDialog();

    }

}
